package com.example.apidemo.dynamic_sql


import com.example.apidemo.dynamic_sql.bean.AttributeItemEntity
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*


/**
 * 数据库操作创建表和删除表
 *
 * @RequestMapping  get post 都可以访问
 * @GetMapping      get 可以访问
 * @PostMapping     post 可以访问
 *
 */

@RestController
@RequestMapping(value = ["/sql"])
class sqlController {

    var db: DbStructureService? = null

    @Autowired
    fun UserController(userService: DbStructureService) {
        this.db = userService
    }


    @RequestMapping(value = ["/inder"])
    @ResponseBody
    fun toInder(): String {
        println("调用 kotlin")
        return "index"
    }

    @RequestMapping(value = ["/dropTables"])
    @ResponseBody
    fun dropTables(): String {
        println("删除 dropTables")

        val list2 = arrayListOf<Long>()
        list2.add(0L)
        list2.add(1L)
        list2.add(2L)
        val isbooi = db?.dropTables(list2)
        if (isbooi!!) {
            return "成功>>>  dropTables"
        }

        return "dropTables"
    }

    @RequestMapping(value = ["/createTable"])
    @ResponseBody
    fun createTable(@RequestParam("int") int: Long = 0L): String {
        println("创建 createTable")

        val list = arrayListOf<AttributeItemEntity>()
        val bean = AttributeItemEntity()
        bean.name = "ceshi"
        bean.id = int.toInt()
        bean.type = "int"
        bean.length = 5
        bean.enableNull = true
        bean.alias = "xx"

        list.add(bean)

        var isbooi = false

        isbooi = db?.existenceTable(AppConst.TABLE_PREFIX + int)!!

        if (isbooi) {
            return "已存在表 ${AppConst.TABLE_PREFIX + int}"
        }

        isbooi = db?.createTable(int, list)!!
        if (isbooi) {
            return "成功>>>  createTable"
        }

        return "No createTable"
    }

    @RequestMapping(value = ["/tableNames"])
    @ResponseBody
    fun tableNames(): String {
        db?.tableNames()?.forEach {
            println("表名: $it")
        }

        return "查询 tableNames"
    }


}